package com.axonactive.yolo.notification;

import java.util.List;

import com.axonactive.yolo.domain.SubscribedFlight;
import com.axonactive.yolo.domain.TaskUserId;
import com.axonactive.yolo.dto.Flight;
import com.axonactive.yolo.monitor.ServiceProvider;
import com.axonactive.yolo.service.interfaces.ExchangeRateService;
import com.axonactive.yolo.util.CONSTANTS;
import com.axonactive.yolo.util.Utils;

public class SMSTemplate implements Template{

	private ExchangeRateService exchangeRateService;

	public SMSTemplate(){
		this.exchangeRateService=(ExchangeRateService) ServiceProvider.getInstance().getService(ExchangeRateService.class);
	}

	private String getFromTo(SubscribedFlight subscribedFlight){
		return subscribedFlight.getOutboundCode()+"-"+subscribedFlight.getOutboundName()
				+" > "+subscribedFlight.getInboundCode()+"-"+subscribedFlight.getInboundName();
	}

	@Override
	public String getCanceledFlightBody(SubscribedFlight subscribedFlight) {
		String flightNumber=subscribedFlight.getOutBoundFlightNumber();
		String fromTo=getFromTo(subscribedFlight);
		String day=subscribedFlight.getOutBoundDate();
		String msg="Your flight "+flightNumber+": "+fromTo +" ("+ day+") has been cancelled";
		return msg;
	}

	@Override
	public String getChangePriceFlightBody(SubscribedFlight subscribedFlight, String currencyCode) {
		String flightNumber=subscribedFlight.getOutBoundFlightNumber();
		String fromTo=getFromTo(subscribedFlight);
		String day=subscribedFlight.getOutBoundDate();
		double priceInRub=subscribedFlight.getPrice();
		double priceInRubBefore=subscribedFlight.getPriceBefore();
		String msg="Your flight "+flightNumber+": "+ fromTo +" ("+day+") has been changed price from "+
				Utils.roundNDigit(exchangeRateService.convert(priceInRubBefore, subscribedFlight.getCurrencyCode(), currencyCode), 2)+" "+currencyCode +" to " +
				Utils.roundNDigit(exchangeRateService.convert(priceInRub, subscribedFlight.getCurrencyCode(), currencyCode), 2)+" "+currencyCode;
		return msg;
	}

	@Override
	public String getChangeScheduleFlightBody(SubscribedFlight subscribedFlight) {
		String flightNumber=subscribedFlight.getOutBoundFlightNumber();
		String fromTo=getFromTo(subscribedFlight);
		String day=subscribedFlight.getOutBoundDate();
		String msg=" You flight "+flightNumber+": "+fromTo+" ("+day+"), "+getChange(subscribedFlight);
		return msg;
	}

	private String getChange(SubscribedFlight subscribedFlight){
		String msg="";
		if (!subscribedFlight.getOutBoundDepartureTime().equals(subscribedFlight.getOutBoundDepartureTimeBefore())){
			msg+=" Departure time changed from "+subscribedFlight.getOutBoundDepartureTimeBefore()+"to"+subscribedFlight.getOutBoundDepartureTime();
		}
		if (!subscribedFlight.getOutBoundArrivalTime().equals(subscribedFlight.getOutBoundArrivalTimeBefore())){
			msg+=" Arrival time changed from "+subscribedFlight.getOutBoundArrivalTimeBefore()+"to"+subscribedFlight.getOutBoundArrivalTime();
		}
		if (CONSTANTS.ROUND_TRIP.equals(subscribedFlight.getTypeOfTrip())){
			if (!subscribedFlight.getInBoundDepartureTime().equals(subscribedFlight.getInBoundDepartureTimeBefore())){
				msg+=" Departure time(return) changed from "+subscribedFlight.getInBoundDepartureTimeBefore()+"to"+subscribedFlight.getInBoundDepartureTime();
			}
			if (!subscribedFlight.getInBoundArrivalTime().equals(subscribedFlight.getInBoundArrivalTimeBefore())){
				msg+=" Arrival time(return) changed from "+subscribedFlight.getInBoundArrivalTimeBefore()+"to"+subscribedFlight.getInBoundArrivalTime();
			}
		}
		return msg;
	}
	
	@Override
	public String getFlightFromTaskBody(List<Flight> flights, TaskUserId taskUserId) {
		String msg = "Your task \"" + taskUserId.getName() +"\" has found flights: " ;
		
		if (flights.size() == 0) {
			return "";
		}
		
		for (int i = 0; i < flights.size() && i <= 5; i++) {
			msg += flights.get(i).getOutBound().getDepartAirports().get(0).getAirline().getFlightNumber() + " - "
					+ flights.get(i).getPrice().getAmount() + flights.get(i).getPrice().getCurrencyCode() + "; ";
		}
		
		msg = msg.substring(0, msg.length() - 2);
		msg += ". Please check your email for more detail.";
		return msg;
	}
}
